<?php

namespace App\Domain\Account\Services;

use App\Domain\Account\Dao\AccountDao;
use App\Domain\Account\Models\AccountModel;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use App\Exceptions\BadRequestException;
use function create_uuid;

class AccountService
{
    /**
     * 生成盐值
     * @return string
     */
    public function generateSalt()
    {
        return \Str::random(16);
    }

    /**
     * 验证密码是否正确
     * @param $password
     * @param $salt
     * @param $correctPassword
     * @return bool
     */
    public function checkPassword($password, $salt, $correctPassword)
    {
        return $this->encryptionPassword($password, $salt) == $correctPassword;
    }

    /**
     * 加密密码
     * @param $password
     * @param $salt
     * @return string
     */
    public function encryptionPassword($password, $salt)
    {
        return md5($password . $salt);
    }

    function createAccount($param)
    {
        $accountDao = new AccountDao();
        $data = $accountDao->findByAccount($param['account']);
        if ($param['account'] === $data['account']) {
            throw new BadRequestException('账号已存在');
        }
        $res = $accountDao->create([
            'id' => create_uuid(),
            'name' => $param['name'],
            'phone' => $param['phone'],
            'validity_time' => $param['validity_time'],
            'state' => $param['state'],
            'salt' => $salt = $this->generateSalt(),
            'account' => $param['account'],
            'roleName' => $param['roleName'],
            'role_id' => $param['role_id'],
            'nickname' => $param['nickname'],
            'password' => md5('12345678' . $salt)
        ]);
        return $res;
    }

    function updateAccount($param)
    {
        $accountDao = new AccountDao();
        $account = $accountDao->findById($param['id']);
        if (empty($account)) {
            throw new BadRequestException('账号不存在');
        }
        $data = [
            'name' => $param['name'],
            'phone' => $param['phone'],
            'validity_time' => $param['validity_time'],
            'state' => $param['state'],
            'salt' => $salt = $this->generateSalt(),
            'account' => $param['account'],
            'roleName' => $param['roleName'],
            'role_id' => $param['role_id'],
            'nickname' => $param['nickname'],
        ];
        $account = $accountDao->update($account, $data);
        return $account;
    }

    /**
     * 用户列表
     * @param $params
     * @return LengthAwarePaginator|Builder[]|Collection
     */
    public function list($params)
    {
        $query = AccountModel::query()
            ->select('account.*')
            ->where('account.status', 1);
        $query = $query->where(function (Builder $query) use ($params) {
            if (isset($params['account']))
                $query->where('account', 'like', "%{$params['account']}%");
            if (isset($params['name']))
                $query->where('name', 'like', "%{$params['name']}%");
            if (isset($params['phone']))
                $query->where('phone', 'like', "%{$params['phone']}%");
            if (isset($params['nickname']))
                $query->where('nickname', 'like', "%{$params['nickname']}%");
            if (isset($params['roleName']))
                $query->where('roleName', $params['roleName']);
            if (isset($params['state']))
                $query->where('state', $params['state']);
            if (isset($params['start_time']) && !empty($params['start_time']))
                $query->where('validity_time', '>=', $params['start_time']);
            if (isset($params['end_time']) && !empty($params['end_time']))
                $query->where('validity_time', '<=', $params['end_time']);
            if (isset($params['acc_start_time']) && !empty($params['acc_start_time']))
                $query->where('create_time', '>=', $params['acc_start_time']);
            if (isset($params['acc_end_time']) && !empty($params['acc_end_time']))
                $query->where('create_time', '<=', $params['acc_end_time']);
        });
        if (isset($params['pageSize']) && isset($params['currentPage'])) {
            return $query->paginate($params['pageSize'], ['*'], 'page', $params['currentPage']);
        } else {
            return $query->get();
        }
    }


    public function del($id)
    {
        $accountDao = new AccountDao();
        $data = $accountDao->del($id);
        return $data;
    }

    public function changeState($param)
    {
        $accountDao = new AccountDao();
        $account = $accountDao->findById($param['id']);
        if ($account['state'] == 1) {
            $res = ['state' => '0'];
            $account = $accountDao->update($account, $res);
        } else {
            $res = ['state' => '1'];
            $account = $accountDao->update($account, $res);
        }
        return $account;
    }

    public function ResetPassword($id)
    {
        $accountDao = new AccountDao();
        $account = $accountDao->findById($id);
        $data = [
            'salt' => $salt = $this->generateSalt(),
            'password' => md5('12345678' . $salt)
        ];
        $account = $accountDao->update($account, $data);
        if($account){
            return [];
        }

    }
}
